package com.gin.supermarket_inventory_system.inventory.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gin.database.base.BaseFields;
import com.gin.spring.exception.BusinessException;
import com.gin.supermarket_inventory_system.inventory.dao.InventoryReceiptDetailPoDao;
import com.gin.supermarket_inventory_system.inventory.entity.InventoryPo;
import com.gin.supermarket_inventory_system.inventory.entity.InventoryReceiptDetailPo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @since : 2023/10/11 11:44
 * @author : ginstone
 * @version : v1.0.0
 **/
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class InventoryReceiptDetailServiceImpl extends ServiceImpl<InventoryReceiptDetailPoDao, InventoryReceiptDetailPo> implements InventoryReceiptDetailService {
    private final InventoryService inventoryService;

    @Override
    public List<InventoryReceiptDetailPo> countCommodityGroupByDate(long start, long end, long commodityId) {
        final List<InventoryPo> inventories = inventoryService.listByCommodityId(commodityId);
        if (inventories.isEmpty()) {
            throw BusinessException.of(HttpStatus.BAD_REQUEST, "商品名下没有库存");
        }
        final List<Long> inventoryId = inventories.stream().map(BaseFields::getId).toList();
        return countInventoryGroupByDate(start, end, inventoryId);
    }
}
